package com.yonyou.pmclouds.projecttype.process;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.projecttype.entity.ProjectTypeVO;
import com.yonyou.pmclouds.projecttype.mapper.ProjectTypeMapper;

public class ProjectTypeExtendCheckProcessor implements SingleProcessor<ProjectTypeVO> {
    private ProjectTypeMapper mapper;
    public ProjectTypeExtendCheckProcessor(ProjectTypeMapper mapper){
        this.mapper=mapper;
    }
    @Override
    public void process(ProjectTypeVO vo, ProjectTypeVO orig) throws BusinessException {
        //首先，判断scode和sname是否已经存在
        if (vo.getPkParent()==null&&vo.getPkRoot()==null){
            //确定为一级类别，则查询一级类别下的所有对象
            ProjectTypeVO[] projectTypeVO = mapper.queryFirstCategory(vo.getPkTenant());
            if (projectTypeVO!=null){
                //待优化
                for (ProjectTypeVO projectTypeVO1:projectTypeVO){
                   if (vo.getSname().equals(projectTypeVO1.getSname())){
                        throw new BusinessException("项目类型名称已存在");
                    }else if (vo.getScode().equals(projectTypeVO1.getScode())){
                        throw new BusinessException("项目类型编号已存在");
                    }
                }
            }
            //将根类型ID设置为一级类别本身ID
            vo.setPkRoot(vo.getPkProjectType());
        }else{
            ProjectTypeVO[] projectTypeVO = mapper.queryRepetition(vo.getPkParent(), vo.getPkRoot(),vo.getPkTenant());
            if (projectTypeVO!=null){
                for (ProjectTypeVO projectTypeVO1:projectTypeVO){
                if (vo.getSname().equals(projectTypeVO1.getSname())){
                    throw new BusinessException("项目类型名称已存在");
                }else if (vo.getScode().equals(projectTypeVO1.getScode())){
                    throw new BusinessException("项目类型编号已存在");
                }
                }
            }
        }
    }
}
